package com.ruoyi.api.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.product.dto.PageQuery;
import com.ruoyi.product.dto.ProductCartAddParam;
import com.ruoyi.product.service.IProductCartService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/product/cart")
@Api(tags = "商品购物车接口")
public class ApiProductCartController extends ApiBaseController {

    @Autowired
    private IProductCartService productCartService;

    @PostMapping("/add")
    @ApiOperation(value = "新增购物车", notes="param中的priceId属性表示价格id， 我可以通过这个id查找到价格计量方式，原价，现价等信息。addNum表示需要设置的商品收藏数量，是一个最终的值，如" +
            "一开始A商品添加了购物车2个，之后又加了3个，当调用该接口时，addNum就应该传5，且当addNum为0时，会默认删除购物车中该商品对应计量方式的记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id",  paramType = "query")
    })
    public AjaxResult addCart(Long productId, @Valid @RequestBody List<ProductCartAddParam> param) {
        Long userId = checkLogin();
        return productCartService.addCart(param, userId, productId);
    }

    @PostMapping("/del")
    @ApiOperation("删除用户购物车商品")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "productIds", value = "商品id列表" ,required = true, allowMultiple = true)
    )
    public AjaxResult removeCart(@RequestBody List<String> productIds) {
        Long userId = checkLogin();
        return productCartService.removeCart(userId, productIds);
    }

    @PostMapping("/updateStatus")
    @ApiOperation("更新购物车商品信息（选中与否：selectFlag=0未选中，selectFlag=1选中）")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "cartIds", value = "购物车id列表" ,required = true, allowMultiple = true)
    )
    public AjaxResult updateStatus(@RequestBody List<Long> cartIds,Integer selectFlag) {
        Long userId = checkLogin();
        return productCartService.updateProductCartStatus(userId,selectFlag,cartIds);
    }

    @PostMapping("/updateAllStatus")
    @ApiOperation("更新购物车商品信息（选中与否：selectFlag=0全部取消，selectFlag=1全部选中）")
    public AjaxResult updateAllStatus(Integer selectFlag) {
        Long userId = checkLogin();
        return productCartService.updateAllStatus(userId,selectFlag);
    }

    @PostMapping("/updateNum")
    @ApiOperation("更新购物车商品信息（数量）")
    @Deprecated
    public AjaxResult updateStatus(Long cartId,Integer num) {
        Long userId = checkLogin();
        JSONObject object = new JSONObject();
        object.put("sum",productCartService.updateProductCartNum(userId,cartId,num));
        return AjaxResult.success(object);
    }


    @GetMapping("/queryCartForOrder")
    @ApiOperation("查询用户提交订单时购物车选中的产品列表")
    public AjaxResult queryCartForOrder(){
        Long userId = checkLogin();
        return productCartService.queryOrderCart(userId);
    }

    @GetMapping("/query")
    @ApiOperation("查询用户购物车")
    public AjaxResult queryCart(@Valid PageQuery pageQuery){
        Long userId = checkLogin();
        return productCartService.queryCart(userId, pageQuery);
    }

    @GetMapping("/queryNoPage")
    @ApiOperation("查询用户购物车")
    public AjaxResult queryNoPage(){
        Long userId = checkLogin();
        return productCartService.queryCart(userId);
    }

    @GetMapping("/query/num")
    @ApiOperation("查询用户购物车中的商品总数")
    public AjaxResult queryCartNum () {
        Long userId = getApiLoginMember();
        return productCartService.queryCartNum(userId);
    }
}
